package com.msb.bf;

import com.google.common.collect.Lists;

import java.util.List;

/**
 * @author: zhaoyi
 * @create: 2020-05-09 11:14
 * @description: 停止一个线程
 **/

public class StopThread {

    public static void main(String[] args) throws InterruptedException {
        final Object lock = new Object();

        List<Integer> list = Lists.newArrayList();

        Thread t0 = new Thread(() -> {
            synchronized (lock) {
                System.out.println("thread->" + Thread.currentThread().getName() + " acquire lock.");
                try {
                    // sleep for 3s
                    Thread.sleep(3000);
                    System.out.println("thread->" + Thread.currentThread().getName() + " add 1");
                    list.add(1);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        });


        Thread t1 = new Thread(() -> {
            synchronized (lock) {
                System.out.println("thread->" + Thread.currentThread().getName() + " acquire lock.");
                list.add(2);
            }
        });

        t0.start();
        Thread.sleep(1000);

        t0.stop();
        t1.start();

        list.forEach(System.out::println);//只有2
    }
}


